from sqlalchemy import PrimaryKeyConstraint, Sequence
from sqlalchemy.dialects.postgresql import TIMESTAMP

from tzq.extensions import db


class Property(db.Model):
    __tablename__ = 'dm_property_t'
    __table_args__ = (
        PrimaryKeyConstraint('property_id', name='dm_property_pk', comment='主键约束'),
        {'comment': '数据字典表'},
    )
    property_id = db.Column(
        db.BigInteger,
        Sequence('dm_property_s'),
        primary_key=True,
        server_default=Sequence('dm_property_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    app_name = db.Column(db.String(2000), nullable=False, comment='应用名称')
    name = db.Column(db.String(2000), nullable=False, comment='名称')
    value = db.Column(db.String(2000), nullable=False, comment='值')
    display_name = db.Column(db.String(2000), nullable=False, comment='显示名称')
    parent_path = db.Column(db.String(2000), nullable=False, comment='父节点的路径')
    description = db.Column(db.String(2000), comment='描述信息')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'property_id': self.property_id,
            # 业务字段
            'app_name': self.app_name,
            'name': self.name,
            'value': self.value,
            'display_name': self.display_name,
            'parent_path': self.parent_path,
            'description': self.description,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }
